home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Gamer Resource Kit / Hardcore Gamer Resource Kit - Disc 2.iso / Utils / UNIX / UNZIP520 / WINGUI / ACTION.C < prev    next >
C/C++ Source or Header  |  1996-04-29  |  14KB  |  457 lines

  1. /* Action.c module of WizUnZip.
  2.  * Author: Robert A. Heath, 1993
  3.  * I, Robert Heath, place this source code module in the public domain.
  4.  *
  5.  * Modifications: Mike White 1995
  6.  */
  7.  
  8. #define UNZIP_INTERNAL
  9. #include "unzip.h"
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #ifndef  WIN32
  14. #include <dos.h>
  15. #endif
  16. #include "wingui\wizunzip.h"
  17.  
  18. #define cchFilesMax 4096
  19. char rgszFiles[cchFilesMax]; /* Moved out of action to allow for larger number
  20.                                 of files to be selected. */
  21. unsigned long dSpaceToUse;
  22.  
  23. char __based(__segname("STRINGS_TEXT")) szNoMemory[] =
  24.             "Insufficient memory for this operation!";
  25.  
  26. char __based(__segname("STRINGS_TEXT")) szCantChDir[] =
  27.          "Can't change directory to %s!";
  28.  
  29. /* Get Selection Count returns a count of the selected
  30.  * list box items. If the count is  greater than zero, it also returns
  31.  * a pointer to a locked list in local memory of the item nos.
  32.  * and their local memory handle.
  33.  * A value of -1 indicates an error.
  34.  */
  35. int CLBItemsGet(HWND hListBox, int __far * __far *ppnSelItems, HANDLE *phnd)
  36. {
  37. int cSelLBItems = (int)SendMessage(hListBox, LB_GETSELCOUNT, 0, 0L);
  38.  
  39. if ( !phnd )
  40.    return -1;
  41.  
  42. *phnd = 0;
  43. if (cSelLBItems)
  44.    {
  45.    *phnd = GlobalAlloc(GMEM_FIXED, cSelLBItems * sizeof(int));
  46.    if ( !*phnd )
  47.       return -1;
  48.  
  49.    *ppnSelItems = (int __far *)GlobalLock( *phnd );
  50.    if ( !*ppnSelItems )
  51.       {
  52.       GlobalFree( *phnd );
  53.       *phnd = 0;
  54.       return -1;
  55.       }
  56.  
  57.    /* Get list of selected items. Return value is number of selected items */
  58.    if (SendMessage(hWndList, LB_GETSELITEMS, cSelLBItems, (LONG)*ppnSelItems) != cSelLBItems)
  59.       {
  60.       GlobalUnlock(*phnd);
  61.       GlobalFree(*phnd);
  62.       *phnd = 0;
  63.       return -1;
  64.       }
  65.    }
  66.    return cSelLBItems;
  67. }
  68.  
  69. /* Re-select listbox contents from given list. The pnSelItems is a
  70.  * list containing the indices of those items selected in the listbox.
  71.  * This list was probably created by GetLBSelCount() above.
  72.  */
  73. void ReselectLB(HWND hListBox, int cSelLBItems, int __far *pnSelItems)
  74. {
  75. int i;
  76.  
  77. for (i = 0; i < cSelLBItems; ++i)
  78.    {
  79.    SendMessage(hListBox, LB_SETSEL, TRUE, MAKELPARAM(pnSelItems[i],0));
  80.    }
  81. }
  82.  
  83.  
  84. /* Action is called on double-clicking, or selecting one of the 3
  85.  * main action buttons. The action code is the action
  86.  * relative to the listbox or the button ID.
  87.  */
  88. void Action(HWND hWnd, WPARAM wActionCode)
  89. {
  90. HANDLE  hMem;
  91. int i;
  92. int iSelection;
  93. int cch;
  94. int cchCur;
  95. int cchTotal;
  96. unsigned long dFreeSpace;
  97. int __far *pnSelItems;  /* pointer to list of selected items */
  98. #ifndef WIN32
  99. struct diskfree_t df;
  100. #else
  101. DWORD SectorsPerCluster, BytesPerSector, FreeClusters, Clusters;
  102. #endif
  103. HANDLE  hnd = 0;
  104. int cSelLBItems = CLBItemsGet(hWndList, &pnSelItems, &hnd);
  105. int argc;
  106. LPSTR   lpszT;
  107. char **pszIndex;
  108. char *sz;
  109. WORD wIndex = (WORD)(!uf.fFormatLong ? SHORT_FORM_FNAME_INX
  110.    : LONG_FORM_FNAME_INX);
  111.  
  112. gfCancelDisplay = FALSE;   /* clear any previous cancel */
  113.     /* if no items were selected */
  114. if (cSelLBItems < 1)
  115.    return;
  116.  
  117.     /* Note: this global value can be overriden in replace.c */
  118. uf.fDoAll = (lpDCL->Overwrite) ? 1 : 0;
  119.  
  120. SetCapture(hWnd);
  121. hSaveCursor = SetCursor(hHourGlass);
  122. ShowCursor(TRUE);
  123.  
  124.     /* If all the files are selected pass in no filenames */
  125.     /* since unzipping all files is the default */
  126. hMem = GlobalAlloc( GPTR, 4096 );
  127. if ( !hMem )
  128.    goto done;
  129. lpszT = (LPSTR)GlobalLock( hMem );
  130. if ( !lpszT )
  131.    {
  132.    GlobalFree( hMem );
  133.    goto done;
  134.    }
  135.  
  136. argc = ((WORD)cSelLBItems == cZippedFiles) ? 0 : 1;
  137. iSelection = 0;
  138.  
  139. do
  140.    {
  141.  
  142.    if (argc)
  143.       {
  144.       cchCur = 0;
  145.       dSpaceToUse = 0;
  146.       pszIndex = (char **)rgszFiles;
  147.       cch = (sizeof(char *) * ((cSelLBItems > (cchFilesMax/16)-1 ) ? (cchFilesMax/16) : cSelLBItems+1));
  148.       cchTotal = (cchFilesMax-1) - cch;
  149.       sz = rgszFiles + cch;
  150.  
  151.       for (i=0; ((i+iSelection)<cSelLBItems) && (i<(cchFilesMax/16)-1); ++i)
  152.          {
  153.          cch = (int)SendMessage(hWndList, LB_GETTEXTLEN, pnSelItems[i+iSelection], 0L);
  154.          if (cch != LB_ERR)
  155.             {
  156.             if ((cchCur+cch+1-wIndex) > cchTotal)
  157.                break;
  158.             cch = (int)SendMessage(hWndList, LB_GETTEXT, pnSelItems[i+iSelection], (LONG)lpszT);
  159.             if ((cch != LB_ERR) && (cch>wIndex))
  160.                {
  161.                /* Get uncompressed totals to pre-flight the extraction process */
  162.                strncpy(sz, lpszT, 9);
  163.                sz[9] = '\0';
  164.                dSpaceToUse += atol(sz);
  165.                lstrcpy(sz, lpszT+wIndex);
  166.                pszIndex[i] = sz;
  167.                cchCur += (cch + 1 - wIndex);
  168.                sz += (cch + 1 - wIndex);
  169.                }
  170.             else
  171.                {
  172.                break;
  173.                }
  174.             }
  175.          else
  176.             {
  177.             MessageBeep(1);
  178.             goto done;
  179.             }
  180.          }
  181.       if (i == 0)
  182.          goto done;
  183.       argc = i;
  184.  
  185.       pszIndex[i] = 0;
  186.       iSelection += i;
  187.       }
  188. else
  189.    {
  190.    iSelection = cSelLBItems;
  191.    }
  192.  
  193. switch (wActionCode)
  194.    {
  195. #ifdef WIN32
  196.     char tempChar;
  197. #endif
  198.     int Ok;
  199.      case 0:         /* extract button */
  200.          /* Get amount of free space on target drive */
  201. #ifndef WIN32
  202.        _dos_getdiskfree((lpumb->szUnzipToDirName[0] - 'A'+1), &df);
  203.        dFreeSpace = (long)df.avail_clusters *
  204.                     ((long)df.sectors_per_cluster *
  205.                     (long)df.bytes_per_sector);
  206. #else
  207.       tempChar = lpumb->szUnzipToDirName[3];
  208.       lpumb->szUnzipToDirName[3] = '\0';
  209.       GetDiskFreeSpace(lpumb->szUnzipToDirName, &SectorsPerCluster, &BytesPerSector, &FreeClusters, &Clusters);
  210.       lpumb->szUnzipToDirName[3] = tempChar;
  211.       dFreeSpace = (long)SectorsPerCluster * (long)BytesPerSector * (long)FreeClusters;
  212. #endif
  213.       if (dFreeSpace < dSpaceToUse)
  214.          {
  215.          sprintf(sz, "Free Disk Space = %lu bytes\nUncompressed Files Total %lu bytes\n",
  216.             dFreeSpace, dSpaceToUse);
  217.          Ok = MessageBox(hWndMain, sz, "Insufficient Disk Space?", MB_OKCANCEL |
  218.             MB_ICONEXCLAMATION);
  219.          }
  220.       else
  221.          Ok = IDOK;
  222.       if (Ok != IDOK)
  223.          break;
  224.       lpDCL->ncflag = 0;
  225.       lpDCL->ntflag = 0;
  226.       lpDCL->nvflag = 0;
  227.       lpDCL->nUflag = lpDCL->ExtractOnlyNewer;
  228.       lpDCL->nzflag = 0;
  229.       lpDCL->ndflag = (int)(uf.fRecreateDirs ? 1 : 0);
  230.       lpDCL->noflag = uf.fDoAll;
  231.       lpDCL->naflag = (int)(uf.fTranslate ? 1 : 0);
  232.       lpDCL->argc   = argc;
  233.         lpDCL->lpszZipFN = lpumb->szFileName;
  234.       lpDCL->FNV = (char **)rgszFiles;
  235. #ifdef USEWIZUNZDLL
  236.       {
  237.       int DlgDirListOK = 1; /* non-zero when DlgDirList() succeeds */
  238.             /* If extracting to different directory from archive dir.,
  239.              * temporarily go to "unzip to" directory.
  240.              */
  241.       if (!uf.fUnzipToZipDir && lpumb->szUnzipToDirName[0])
  242.          {
  243.          lstrcpy(lpumb->szBuffer, lpumb->szUnzipToDirName); /* OK to clobber szBuffer! */
  244.          DlgDirListOK = DlgDirList(hWnd, lpumb->szBuffer, 0, 0, 0);
  245.          }
  246.       if (!DlgDirListOK)   /* if DlgDirList failed  */
  247.          {
  248.          wsprintf(lpumb->szBuffer, szCantChDir, lpumb->szUnzipToDirName);
  249.          MessageBox(hWndMain, lpumb->szBuffer, NULL, MB_OK | MB_ICONEXCLAMATION);
  250.          }
  251.       else
  252.          {
  253.             (*DllProcessZipFiles)(lpDCL);
  254.          if (!uf.fUnzipToZipDir && lpumb->szUnzipToDirName[0])
  255.             {
  256.             lstrcpy(lpumb->szBuffer, lpumb->szDirName); /* OK to clobber szBuffer! */
  257.             if (!DlgDirList(hWnd, lpumb->szBuffer, 0, 0, 0)) /* cd back */
  258.                {
  259.                wsprintf(lpumb->szBuffer, szCantChDir, lpumb->szDirName);
  260.                MessageBox(hWndMain, lpumb->szBuffer, NULL, MB_OK | MB_ICONEXCLAMATION);
  261.                }
  262.             }
  263.          }
  264.          }
  265. #else
  266.          if (FSetUpToProcessZipFile(lpDCL))
  267.          {
  268.          int DlgDirListOK = 1; /* non-zero when DlgDirList() succeeds */
  269.  
  270.             /* If extracting to different directory from archive dir.,
  271.              * temporarily go to "unzip to" directory.
  272.              */
  273.          if (!uf.fUnzipToZipDir && lpumb->szUnzipToDirName[0])
  274.             {
  275.             lstrcpy(lpumb->szBuffer, lpumb->szUnzipToDirName); /* OK to clobber szBuffer! */
  276.             DlgDirListOK = DlgDirList(hWnd, lpumb->szBuffer, 0, 0, 0);
  277.             }
  278.          if (!DlgDirListOK)   /* if DlgDirList failed  */
  279.             {
  280.             wsprintf(lpumb->szBuffer, szCantChDir, lpumb->szUnzipToDirName);
  281.             MessageBox(hWndMain, lpumb->szBuffer, NULL, MB_OK | MB_ICONEXCLAMATION);
  282.             }
  283.          else
  284.             {
  285.             process_zipfiles(__G);   /* extract the file(s)         */
  286.                /* Then return to archive dir. after extraction.
  287.                 * (szDirName is always defined if archive file defined)
  288.                 */
  289.             if (!uf.fUnzipToZipDir && lpumb->szUnzipToDirName[0])
  290.                {
  291.                lstrcpy(lpumb->szBuffer, lpumb->szDirName); /* OK to clobber szBuffer! */
  292.                if (!DlgDirList(hWnd, lpumb->szBuffer, 0, 0, 0)) /* cd back */
  293.                   {
  294.                   wsprintf(lpumb->szBuffer, szCantChDir, lpumb->szDirName);
  295.                   MessageBox(hWndMain, lpumb->szBuffer, NULL, MB_OK | MB_ICONEXCLAMATION);
  296.                   }
  297.                }
  298.             }
  299.             }
  300.             else
  301.  
  302.                 /* FSetUpToProcessZipFile failed */
  303.                {
  304.                 MessageBox(hWndMain, szNoMemory, NULL, MB_OK | MB_ICONEXCLAMATION);
  305.                }
  306.             TakeDownFromProcessZipFile();
  307. #endif
  308.             break;
  309.         case 1:     /* display to message window */
  310.             bRealTimeMsgUpdate = FALSE;
  311.             lpDCL->ncflag = 1;
  312.             lpDCL->ntflag = 0;
  313.             lpDCL->nvflag = 0;
  314.             lpDCL->nUflag = 1;
  315.             lpDCL->nzflag = 0;
  316.             lpDCL->ndflag = 0;
  317.             lpDCL->noflag = 0;
  318.             lpDCL->naflag = 0;
  319.             lpDCL->argc   = argc;
  320.                 lpDCL->lpszZipFN = lpumb->szFileName;
  321.             lpDCL->FNV = (char **)rgszFiles;
  322. #ifdef USEWIZUNZDLL
  323.                 (*DllProcessZipFiles)(lpDCL);
  324. #else
  325.             if (FSetUpToProcessZipFile(lpDCL))
  326.             {
  327.                 process_zipfiles(__G);
  328.             }
  329.             else
  330.             {
  331.                 MessageBox(hWndMain, szNoMemory, NULL, MB_OK | MB_ICONEXCLAMATION);
  332.             }
  333.  
  334.             TakeDownFromProcessZipFile();
  335. #endif
  336.             bRealTimeMsgUpdate = TRUE;
  337.          if (uf.fAutoClearStatus)   /* if automatically clearing status, leave user at top */
  338.             SetStatusTopWndPos();
  339.  
  340.          else   /* traditional behavior leaves user at bottom of window */
  341.             UpdateMsgWndPos();
  342.  
  343.          /* Following extraction to status window, user will want
  344.           * to scroll around, so leave him/her on Status window.
  345.           */
  346.            if (wWindowSelection != IDM_MAX_LISTBOX)
  347.               PostMessage(hWnd, WM_COMMAND, IDM_SETFOCUS_ON_STATUS, 0L);
  348.  
  349.             break;
  350.         case 2:     /* test */
  351.             lpDCL->ncflag = 0;
  352.             lpDCL->ntflag = 1;
  353.             lpDCL->nvflag = 0;
  354.             lpDCL->nUflag = 1;
  355.             lpDCL->nzflag = 0;
  356.             lpDCL->ndflag = 0;
  357.             lpDCL->noflag = 0;
  358.             lpDCL->naflag = 0;
  359.                 lpDCL->argc   = argc;
  360.                 lpDCL->lpszZipFN = lpumb->szFileName;
  361.             lpDCL->FNV = (char **)rgszFiles;
  362. #ifdef USEWIZUNZDLL
  363.                 (*DllProcessZipFiles)(lpDCL);
  364. #else
  365.             if (FSetUpToProcessZipFile(lpDCL))
  366.             {
  367.                 process_zipfiles(__G);
  368.             }
  369.             else
  370.             {
  371.                 MessageBox(hWndMain, szNoMemory, NULL, MB_OK | MB_ICONEXCLAMATION);
  372.             }
  373.  
  374.             TakeDownFromProcessZipFile();
  375. #endif
  376.             break;
  377.           }
  378.     } while (iSelection < cSelLBItems);
  379.  
  380.  
  381.     /* march through list box checking what's selected
  382.      * and what is not.
  383.      */
  384.  
  385. done:
  386.  
  387.     if ( hMem )
  388.     {
  389.         GlobalUnlock( hMem );
  390.         GlobalFree( hMem );
  391.     }
  392.     GlobalUnlock(hnd);
  393.     GlobalFree(hnd);
  394.  
  395.  
  396.     ShowCursor(FALSE);
  397.     SetCursor(hSaveCursor);
  398.     ReleaseCapture();
  399.    SoundAfter();      /* play sound afterward if requested */
  400.     if (!uf.fIconSwitched)  /* if haven't already, switch icons */
  401.     {
  402.         HANDLE hIcon;
  403.  
  404.         hIcon = LoadIcon(hInst,"UNZIPPED"); /* load final icon   */
  405.         assert(hIcon);
  406. #ifndef WIN32
  407.         SetClassWord(hWndMain, GCW_HICON, hIcon);
  408. #else
  409.         SetClassLong(hWndMain, GCL_HICON, (LONG)hIcon);
  410. #endif
  411.         uf.fIconSwitched = TRUE;    /* flag that we've switched it  */
  412.     }
  413. }
  414.  
  415. /* Display the archive comment using the Info-ZIP engine. */
  416. void DisplayComment(HWND hWnd)
  417. {
  418.  
  419.    SetCapture(hWnd);
  420.    hSaveCursor = SetCursor(hHourGlass);
  421.    ShowCursor(TRUE);
  422.    bRealTimeMsgUpdate = FALSE;
  423.    gfCancelDisplay = FALSE;   /* clear any previous cancel */
  424.    /* Called here when showing zipfile comment */
  425.    lpDCL->ncflag = 0;
  426.    lpDCL->ntflag = 0;
  427.    lpDCL->nvflag = 0;
  428.    lpDCL->nUflag = 1;
  429.    lpDCL->nzflag = 1;
  430.    lpDCL->ndflag = 0;
  431.    lpDCL->noflag = 0;
  432.    lpDCL->naflag = 0;
  433.    lpDCL->argc   = 0;
  434.     lpDCL->lpszZipFN = lpumb->szFileName;
  435.    lpDCL->FNV = NULL;
  436. #ifdef USEWIZUNZDLL
  437.    (*DllProcessZipFiles)(lpDCL);
  438. #else
  439.    if (FSetUpToProcessZipFile(lpDCL))
  440.     {
  441.         process_zipfiles(__G);
  442.     }
  443.     else
  444.     {
  445.         MessageBox(hWndMain, szNoMemory, NULL, MB_OK | MB_ICONEXCLAMATION);
  446.     }
  447.  
  448.    TakeDownFromProcessZipFile();
  449. #endif
  450.    ShowCursor(FALSE);
  451.    SetCursor(hSaveCursor);
  452.    bRealTimeMsgUpdate = TRUE;
  453.    UpdateMsgWndPos();
  454.    ReleaseCapture();
  455.    SoundAfter();      /* play sound during if requested         */
  456. }
  457.